package com.cy.java.oop;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class ReflectTests {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
//        Class<Point> pointClass = Point.class;
        Class<?> c1 = Class.forName("com.cy.java.oop.Point");
        Field x = c1.getDeclaredField("x");
        Constructor<?> constructor = c1.getConstructor(int.class, int.class);
        Object p= constructor.newInstance(10,20);
        System.out.println(p);
        x.setAccessible(true);
        x.set(p,100);
        System.out.println(p);
        Object o = x.get(p);
        System.out.println(o);

    }
}
class Point{
    private int x;
    private int y;
    public Point(int x,int y){
        this.x=x;
        this.y=y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}